package com.tools.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.tools.dao.QueryDao;
import com.tools.dao.QueryDetailDao;
import com.tools.entity.Query;
import com.tools.entity.QueryDetail;
import com.tools.service.QueryService;

@Service
@Transactional
public class QueryServiceImpl implements QueryService {

	@Autowired
	private QueryDao queryDao;
	@Autowired
	private QueryDetailDao queryDetailDao;
	public List<Query> getAll() {
		// TODO Auto-generated method stub
		return queryDao.getAll();
	}
	public Query fetchById(int id) {
		// TODO Auto-generated method stub
		return queryDao.fetchById(id);
	}
	@Transactional(propagation=Propagation.REQUIRED,rollbackFor=Exception.class)
	public boolean insert(Query query) {
		// TODO Auto-generated method stub
		boolean boo=queryDao.insert(query)>0;
		for (QueryDetail queryDetail : query.getQueryDetails()) {
			queryDetail.setQuery(query);
			boo=boo&&queryDetailDao.insert(queryDetail)>0;
		}
		return boo;
	}
	@Transactional(propagation=Propagation.REQUIRED,rollbackFor=Exception.class)
	public boolean delete(int id) {
		// TODO Auto-generated method stub
		boolean boo=queryDao.delete(id)>0;
		queryDetailDao.delete(id);
		return boo;
	}
	@Transactional(propagation=Propagation.REQUIRED,rollbackFor=Exception.class)
	public boolean update(Query query) {
		// TODO Auto-generated method stub
		boolean boo=queryDao.update(query)>0;
		boo=boo&&this.delete(query.getId());
		for (QueryDetail queryDetail : query.getQueryDetails()) {
			queryDetail.setQuery(query);
			boo=boo&&queryDetailDao.insert(queryDetail)>0;
		}
		return boo;
	}

}
